Ternary expressions, while concise, can often lead to code that is difficult to read and understand, especially when they are nested or complex.
Prioritizing readability fosters maintainability and reduces the likelihood of bugs. Therefore, they should be removed in favor of more explicit
control structures, such as if
/else
statements, to improve the clarity and readability of the code.
Code examples
Noncompliant code example
function foo(a) {
var b = (a === 'A') ? 'is A' : 'is not A'; // Noncompliant
// ...
}
Compliant solution
function foo(a) {
var b;
if (a === 'A') {
b = 'is A';
}
else {
b = 'is not A';
}
// ...
}